<?php
function user_registed(){
	global $user;
	$query = "SELECT id FROM " .
    "{jindiaojigou_info} where userid = ?";
 	$queryResult = db_query($query, array($user->uid));
	$registed = FALSE;
	$pageid ='';
	foreach ($queryResult as $links ) {
		$pageid =$links->id;
	}
	if($pageid > 0)
		$registed = TRUE;
	return $registed;
}
function get_jindiaojigou_field() {
	return array(
		array("danwei","单位名称","str"),
		array("lianxiren","联系人","str"),
		array("zhiwei","职位","str"),
		array("zuoji","座机","str"),
		array("shouji","手机","str"),
		array("youxiangdizhi","邮箱地址","str"),
		array("zhuanyelingyu","专业领域","txt"),
		array("remark","备注","txt"),
	);
}
function jindiaojigou_list_page() {
	$fields = get_jindiaojigou_field();
	$fields = array_slice($fields,0,6);
	$header = array();
	$query_fields = array('id');
	foreach($fields as $field){
		$header[] = array('data' => $field[1], 'field' => $field[0]);
		$query_fields[] = $field[0];
	}
	$query = db_select("jindiaojigou_info", "n")->extend('PagerDefault')->limit(15)->extend('TableSort')
	->orderByHeader($header)->fields('n',$query_fields);
	$results = $query->execute();
	$record = $results->fetchAssoc();
	$rows = array();
	while ($record != FALSE) {
		$line = array('data' => array(l($record["danwei"],'jindiaojigou/edit/'.$record["id"])));
		for ($i=1; $i < 6; $i++) { 
			$line['data'][] = $record[$fields[$i][0]];
		}
		$rows[] = $line;
		$record = $results->fetchAssoc();
	}
	$html = theme('table',array(
		'header' => $header,
		'rows'=>$rows,
		'sticky' => TRUE,
		'empty' => '没有尽调机构信息',
    ));
	$html .= theme('pager',array('tags' => array()));
	return ($html); 
}
function get_jindiaojigou_by_id($id) {
	$query = "SELECT * FROM {jindiaojigou_info} WHERE id = ?";
	$query_result = db_query($query,array($id))->fetchAssoc();
	return $query_result;
}
function jindiaojigou_info_page($id) {
	$html = "";
	$html .= l('查看尽调机构列表','jindiaojigou/list').'<br/><br/>';
	$fields = get_jindiaojigou_field();
	$info = get_jindiaojigou_by_id($id);
	if($info == FALSE){
		$html .= "没有该尽调机构信息";
	}else foreach ($fields as $field) {
		$html .= '<h5>'.$field[1].": ".$info[$field[0]].'</h5><br/>';
	}
	return $html;
}
function edit_form_field($engname,$chiname,$type,$default){
	$field = array("#title" => t($chiname));
	if($type == "str") {
		$field["#type"] = "textfield";
		$field["#required"] = TRUE;
	}elseif($type == "txt") {
		$field["#type"] = "textarea";
		$field["#required"] = FALSE;
	}
	$field["#default_value"] = $default;
	return $field;
}
function jindiaojigou_edit_page($id) {
	$html = array("#markup"=>"");
	if (!user_registed()){
		$form = drupal_get_form("jindiaojigou_edit_form");
		$html["#markup"] .= drupal_render($form);
		return $html;
	}else{
		drupal_goto('jindiaojigou/grade');
	}
}
function jindiaojigou_edit_form($id) {
	$form = array();
	$field = get_jindiaojigou_field();
	$query = "SELECT * FROM {jindiaojigou_info} WHERE id = ?";
	$query_result = db_query($query,array('id'=>$id))->fetchAssoc();
	$form['tips'] = array(
		'#prefix' =>'<div id="tips">',
		'#markup' => t('<span class="form-required">*</span> 号为必填项。'),
		'#suffix' =>'</div>',
	);
	$form['bid'] = array(
		'#value' => $id,
		'#type' => "hidden",
	);
	foreach ($field as $value) {
		$form[$value[0]] = edit_form_field($value[0], $value[1], $value[2], $query_result[$value[0]]);
	}
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('确认'),
	);
	return $form;
}
function jindiaojigou_edit_form_submit($form,&$form_state){
	$values = $form_state['values'];
	$fields = get_jindiaojigou_field();
	$fields_data = array();
	foreach ($fields as $field) {
		$fields_data[$field[0]] = $values[$field[0]];
	}
	$last_id = db_update('jindiaojigou_info')
	->fields($fields_data)
	->condition('id',$values['bid'])
	->execute();
	if ($last_id!=NULL) {
		$form_state['redirect'] = 'jindiaojigou/grade';  
	} else {
		drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	}
}
function jindiaojigou_create_page() {
	$html = array("#markup"=>"");
	if (!user_registed()){
		$form = drupal_get_form("jindiaojigou_create_form");
		$html["#markup"] .= drupal_render($form);
		return $html;
	}else{
		drupal_goto('jindiaojigou/grade');
	}
}
function create_form_field($engname,$chiname,$type,$formname){
	$field = array("#title" => t($chiname));
	if($type == "str") {
		$field["#type"] = "textfield";
		$field["#required"] = TRUE;
	}elseif($type == "txt") {
		$field["#type"] = "textarea";
		$field["#required"] = FALSE;
	}
	$field["#default_value"] = isset($_SESSION[$formname][$engname])?$_SESSION[$formname][$engname]:"";
	return $field;
}
function jindiaojigou_create_form() {
	$form = array();
	$field = get_jindiaojigou_field();
	$form['tips'] = array(
		'#prefix' =>'<div id="tips">',
		'#markup' => t('<span class="form-required">*</span> 号为必填项。'),
		'#suffix' =>'</div>',
	);
	foreach ($field as $value) {
		$form[$value[0]] = create_form_field($value[0], $value[1], $value[2], "jindiaojigou_create_form");
	}
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('确认'),
	);
	return $form;
}
function jindiaojigou_create_form_validate($form, &$form_state){
	if(!preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $form_state['values']['youxiangdizhi'])){
		form_set_error('mail',t('您输入的电子邮件地址格式不正确'));
	}
	if (drupal_strlen($form_state['values']['danwei']) < 2){
		form_set_error('name', '您输入的姓名长度太短，请填写真实姓名。');
	}
}
function jindiaojigou_create_form_submit($form, &$form_state){
	  //把表单的值存放在会话中去，由于这里涉及到了两个不同的表单之间传值。
	 $_SESSION['jindiaojigou_create_form'] = $form_state['values'];
	 //表单重定向到确认页面
	 $form_state['redirect'] = 'jindiaojigou/create/confirm';
}
function jindiaojigou_confirm_page() {
	if(empty($_SESSION['jindiaojigou_create_form'])){
		drupal_goto('jindiaojigou/create');
	}

	$render_array = array(
		'#markup' => '', 
	);
	$form = drupal_get_form('jindiaojigou_confirm_form');
	$render_array['#markup'] .= drupal_render($form);
	return $render_array;
}
function confirm_form_field($engname,$chiname,$type,$formname){
	$field = array("#title" => t($chiname),"#type"=>"item");
	$field["#markup"] = isset($_SESSION[$formname][$engname])?$_SESSION[$formname][$engname]:"wtf";
	return $field;
}
function jindiaojigou_confirm_form() {
	$form = array();
	$fields = get_jindiaojigou_field();
	foreach ($fields as $field) {
		$form[$field[0]] = confirm_form_field($field[0], $field[1], $field[2], "jindiaojigou_create_form");
	}
	$form["back"] = array(
		"#type" => 'submit',
		'#value' => t('返回'),
		'#submit' => array('jindiaojigou_confirm_form_back'),
	);
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('提交'),
	);	
	return $form;
}
function jindiaojigou_confirm_form_back($form,&$form_state){
	$form_state['redirect'] = 'jindiaojigou/create';
}
function jindiaojigou_confirm_form_submit($form,&$form_state){
	$values = NULL;
	if(empty($_SESSION['jindiaojigou_create_form'])){
		$form_state['redirect'] = 'jindiaojigou/create';
	}else{
		$values = $_SESSION['jindiaojigou_create_form'];
		unset($_SESSION['jindiaojigou_create_form']);
	}
	global $user;
	$fields = get_jindiaojigou_field();
	$fields_data = array('userid'=>$user->uid);
	foreach ($fields as $field) {
		$fields_data[$field[0]] = $values[$field[0]];
	}
	$last_id = db_insert('jindiaojigou_info')->fields($fields_data)->execute();
	if ($last_id!=NULL) {
		$form_state['redirect'] = 'jindiaojigou/grade';  
	} else {
		drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	}
}
function jindiaojigou_delete_page($id) {
	
}
function jindiaojigou_choose_page() {
	module_load_include("inc","enterprise_information2",'enterprise_information2.pages');
	$html = '';
	$applyfunds_info = get_part_by_state(5);
	  //插入待申报基金列表
	$html .= "<div><table style='width: 800px'><tr><th colspan=7>待评审基金列表</th></tr>".
	"<tr><td style='width: 10%'>编号</td>".
	"<td style='width: 15%'>拟设基金名称</td>".
	"<td style='width: 15%'>规模(万元)</td>".
	"<td style='width: 15%'>主投领域</td>".
	"<td style='width: 15%'>管理机构</td>".
	"<td style='width: 15%'>联系人</td>".
	"<td style='width: 15%'>联系电话</td></tr>";
	$count = 0;
	foreach ($applyfunds_info as $fund_info ) {
		$count += 1;
		$html .= ' <tr><td >'.$count.'</td>'.
		'<td >'.$fund_info["nishejijinmingcheng"].'</td>'.
		'<td >'.round($fund_info["jijinguimo"]).'</td>'.
		'<td >'.$fund_info["zhuyaotouzilingyu"].'</td>'.
		'<td >'.$fund_info["guanlijigoumingcheng"].'</td>'.
		'<td >'.$fund_info["lianxiren"].'</td>'.
		'<td >'.$fund_info["lianxidianhua"].'</td></tr>';
	}
	$html .= "</table></div><br><br>";
	if($count == 0){
		$html = '当前没有待评审申报基金。';
		$render_array = array('#markup' => $html);
		return $render_array;
	} 
	$query = 'SELECT * FROM {pici} WHERE state = ?';
	$query_result = db_query($query,array(3))->fetchAssoc();
	if($query_result == FALSE)
	{
		module_load_include('inc','expert_pici_manage','expert_pici_manage.pages');
		get_currentpici_investigate_list();
		$form = drupal_get_form("jindiaojigou_choose_form");
        $html .= '<div>尽调机构选择（上面的表格为财务尽调机构选择，下面的表格为法律尽调机构选择）</div>';
		$html .= drupal_render($form);
		$render_array = array('#markup' => $html);
	}else{
		if($query_result['jindiao_remark'] != ''){
			$html .= "<table style='width: 800px'><tr><th colspan=1>评审说明</th></tr><br><br>".
	  				"<tr><td >".$query_result['jindiao_remark'].'</td></tr></table><br><br>';
		}else{
			$html .= "没有填写尽调说明!<br><br>";
		}
		$jindiao_id = $query_result['investigate_list'];
        $jindiao_id_list = explode(",", $jindiao_id);
        $html .= "已选择尽调机构<br>";
        $html .= "<table style='width: 800px'><tr><th>单位</th><th>联系人</th><th>手机</th></tr>";
        for ($i=0; $i<count($jindiao_id_list); $i++) {
		    $query = 'SELECT * FROM {jindiaojigou_info} WHERE id = ?';
		    $query_result = db_query($query,array($jindiao_id_list[$i]))->fetchAssoc();
		    $html .= '<tr><td>'.l($query_result['danwei'],'jindiaojigou/info/'.$jindiao_id_list[$i]);
		    $html .= '</td><td>'.$query_result['lianxiren'].'</td><td>'.$query_result['shouji'].'</td></tr>';
        }
        $html .= '</table>';
		$render_array = array('#markup' => $html);
	}
	return $render_array;
}
function jindiaojigou_choose_form(){
	$fields = array_slice(get_jindiaojigou_field(),0,5);
	$query = 'SELECT jindiao_remark,investigate_list FROM {pici} WHERE state = ?';
	$query_result = db_query($query,array(2))->fetchAssoc();
	$form['jindiao_remark'] = array(
		'#title' => '尽职调查说明',
		'#type' => 'textarea',
		'#default_value' => $query_result['jindiao_remark'],
	);
	$jindiaojigou_choice = $query_result["investigate_list"];
	$query = "SELECT * FROM {jindiaojigou_info}";
	$query_result = db_query($query);
	$record = $query_result->fetchAssoc();
	$jindiaojigou_list = array();
	while($record != FALSE){
		$jindiaojigou_list[] = $record;
		$record = $query_result->fetchAssoc();
	}
	$header = array();
	$header1 = array();
	foreach ($fields as $field) {
		if($field[0] == 'danwei'){
			$header[$field[0]] = t('会计师事务所');
			$header1[$field[0]] = t('律师事务所');
		}else{
			$header[$field[0]] = t($field[1]);
			$header1[$field[0]] = t($field[1]);
		}
	}
	$options = array();
	foreach($jindiaojigou_list as $info){
		$options[$info['id']] = array();
		$options[$info['id']]['danwei'] = l($info['danwei'],'jindiaojigou/info/'.$info['id']);
		for ($i=1; $i < 5; $i++) { 
			$options[$info['id']][$fields[$i][0]] = $info[$fields[$i][0]];
		}
	}
	$form['table'] = array(
		'#type' => 'tableselect',
		'#title' => '财务尽调机构',
		'#header' => $header,
		'#options' => $options,
		'#empty' => t('没有尽调机构记录'),
		'#mutiple' => FALSE,
	);
	$form['table1'] = array(
		'#type' => 'tableselect',
		'#title' => '法律尽调机构',
		'#header' => $header1,
		'#options' => $options,
		'#empty' => t('没有尽调机构记录'),
		'#mutiple' => FALSE,
	);
	$form['save'] = array(
		'#type' => 'submit',
		'#value' => t('暂存'),
		'#submit' => array('jindiaojigou_choose_form_save'),
	);
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('提交（不可再修改）'),
	);
	return $form;
}
function jindiaojigou_choose_form_save($form,&$form_state) {
	$values = $form_state['values'];
	$choice = '';
	foreach ($values['table'] as $row) {
		if ($row != '0')
			$choice .= $row;
	}
	$choice1 = '';
	foreach ($values['table1'] as $row) {
		if ($row != '0')
			$choice .= $row;
	}
	$result = db_update('pici')
	->fields(array('investigate_list'=>$choice&$choice1,
		'jindiao_remark' => $values['jindiao_remark'],
	))
	->condition('state',2)
	->execute();
	if($result != NULL){
		$form_state['redirect'] = 'jindiaojigou/choose';
	}else{
		drupal_set_message('抱歉，遇到问题，提交失败了'.$choice, 'error');
	}
}
function jindiaojigou_choose_form_submit($form,&$form_state) {
	$values = $form_state['values'];
	$choice = '';
	foreach ($values['table'] as $row) {
		if ($row != '0')
			$choice .= $row;
	}
	$choice1 = '';
	foreach ($values['table1'] as $row) {
		if ($row != '0')
			$choice1 .= $row;
	}
	$result1 = db_update('pici')
	->fields(array('investigate_list'=>$choice.",".$choice1,
		'jindiao_remark' => $values['jindiao_remark'],
		'state'=>3
	))
	->condition('state',2)
	->execute();
	if($result1 != NULL){
		$form_state['redirect'] = 'jindiaojigou/choose';
	}else{
		drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	}
}
function jindiaojigou_manage_report_page() {
	$form = drupal_get_form('jindiaojigou_manage_report_form');
	$html = drupal_render($form);
	return array('#markup'=>$html);
}
function jindiaojigou_manage_report_form() {
	module_load_include("inc","enterprise_information2",'enterprise_information2.pages');
	$applyfunds_info = get_part_by_state(5);
	$header = array(
		'jijinmingcheng' => t('基金名称'),
		'caozuo' => t('操作'),
	);
	$options = array();
	foreach ($applyfunds_info as $fund_info) {
		$options[$fund_info['id']] = array(
			'jijinmingcheng' => l($fund_info['nishejijinmingcheng'],'enterprise_information2/viewbyid/'.$fund_info['id']),
			'caozuo' => l("查看尽调报告",'jindiaojigou/report_view/'.$fund_info['id']),
		);
	}
	$form['table'] = array(
		'#type' => 'tableselect',
		'#header' => $header,
		'#options' => $options,
		'#empty' => t('没有专家记录'),
	);
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => '提交（将不可再修改)',
	);
	return $form;
}
function jindiaojigou_manage_report_form_submit($form, &$form_state){
	module_load_include("inc","enterprise_information2",'enterprise_information2.pages');
	$values = $form_state['values'];
	$funds_list = array();
	foreach ($values['table'] as $rows) {
		$funds_list[] = $rows;
	}
	$funds_array = get_part_by_state(5);
	foreach ($funds_array as $fund) {
		if(in_array($fund['id'], $funds_list, TRUE)&&$funds_list != NULL){
			set_state_by_id(7, $fund['id']);
		}else{
			set_state_by_id(6,$fund['id']);
		}
	}
	db_update('pici')
  	->fields(array('state' => 4))
  	->condition('state', 3)
  	->execute();
	$form_state['redirect'] = 'jindiaojigou/manage_report';
}
function jindiaojigou_report_view_page($id) {
	$query = "SELECT * FROM {jindiaojigou_verdict} WHERE shenqingjijin_id = ?";
	$query_result = db_query($query,array($id));
	if (count($query_result) == 0)
		return "<p>尽调机构尚未提交尽调报告！</p>";
	$html = "<h2>基金尽调信息</h2><br/><table><tr><td>类型</td><td>综合意见</td><td>风险提示</td><td>查看报告</td></tr>";
    #$html .= "<script>alert('".count($query_result)."');</script>";
	foreach($query_result as $result){
		$html .= "<tr><td>";
		if($result->type == 1)
			$html .= "法律</td>";
		else
			$html .= "财务</td>";
		$html .= "<td>".$result->zongheyijian."</td>";
		$html .= "<td>".$result->fengxiantishi."</td>";
		$html .= "<td>".l($result->file_name, 'http://'.$_SERVER['HTTP_HOST'].'/sites/default/files/'.$result->url)."</td></tr>";
	}
    $html .= '</table>';
	return $html;
}
function jindiaojigou_grade_page() {
	/*
	 * 尽调机构选择基金来提交尽职调查报告
	 */
	global $user;
	$query ='SELECT id FROM {jindiaojigou_info} WHERE userid = ?';
	$current_jigou = db_query($query,array($user->uid))->fetchAssoc();
	if($current_jigou == FALSE)
		return l('请填写尽调机构信息！','jindiaojigou/create');
	module_load_include('inc', 'expert_pici_manage','expert_pici_manage.pages');
	module_load_include("inc", "enterprise_information2","enterprise_information2.pages");	
	$page_content = "";
	$query = 'SELECT investigate_list FROM {pici} WHERE state = ?';
	$query_result = db_query($query,array(3))->fetchAssoc();
	$inv_list = explode(",",$query_result['investigate_list']);
	if($inv_list[0] == $current_jigou['id']){
		$applyfunds_info = get_part_by_state(5);
		$page_content .= "<div><table style='width: 700px'><tr><th colspan=4>待调查基金列表</th></tr>".
		"<tr><td style='width: 20%'>编号</td>".
		"<td style='width: 60%'>拟设基金名称</td>".
		"<td style='width: 20%'>操作</td></tr>";
		$count = 0;
		foreach ($applyfunds_info as $fund_info ) {
			$query = "SELECT id,state FROM " .
           	"{jindiaojigou_verdict} where jindiaojigou_id = ? and shenqingjijin_id =?";
			$if_verdict = '';
			$opinion = '';
	  		$query_result = db_query($query, array($current_jigou['id'], $fund_info['id'] ))->fetchAssoc();
			if($query_result['state']==1)
				continue;
			$count += 1;
			$page_content .= ' <tr><td >'.$count.'</td>'.
			'<td >'.$fund_info['nishejijinmingcheng'].'</td>'.
			'<td >'.l('提交调查报告','jindiaojigou/applycw/'.$fund_info['id']).'</td></tr>';
		}
		$page_content .= "</table></div><br><br>"; 
		$render_array = array(
			'#markup' => $page_content,
		);
		$form = drupal_get_form('jindiaojigou_grade_form');
		$render_array['#markup'] .= drupal_render($form);
	}elseif(count($inv_list) > 1 && $inv_list[1] == $current_jigou['id']){
		$applyfunds_info = get_part_by_state(5);
		$page_content .= "<div><table style='width: 700px'><tr><th colspan=4>待调查基金列表</th></tr>".
		"<tr><td style='width: 20%'>编号</td>".
		"<td style='width: 60%'>拟设基金名称</td>".
		"<td style='width: 20%'>操作</td></tr>";
		$count = 0;
		foreach ($applyfunds_info as $fund_info ) {
			$query = "SELECT id,state FROM " .
           	"{jindiaojigou_verdict} where jindiaojigou_id = ? and shenqingjijin_id =?";
			$if_verdict = '';
			$opinion = '';
	  		$query_result = db_query($query, array($current_jigou['id'], $fund_info['id'] ))->fetchAssoc();
			if($query_result['state']==1)
				continue;
			$count += 1;
			$page_content .= ' <tr><td >'.$count.'</td>'.
			'<td >'.$fund_info['nishejijinmingcheng'].'</td>'.
			'<td >'.l('提交调查报告','jindiaojigou/applyfl/'.$fund_info['id']).'</td></tr>';
		}
		$page_content .= "</table></div><br><br>"; 
		$render_array = array(
			'#markup' => $page_content,
		);
		$form = drupal_get_form('jindiaojigou_grade_form');
		$render_array['#markup'] .= drupal_render($form);
	}else{
		$page_content .= '<h4><center>您当前没有需要审批的基金。</center></h4>';
		$render_array = array('#markup'=> $page_content);
	}
	return $render_array;
}
function jindiaojigou_grade_form($form, &$form_state){
	 $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('提交尽调结果（提交后不可再修改）'),
	  );	
	 return $form;
}
function jindiaojigou_grade_form_submit($form, &$form_state){
	$jindiaojigou_id = get_users_jindiaojigou_id();
	db_update('jindiaojigou_verdict')
  	->fields(array(
   		'state' => 1,
  	))
  	->condition('state', 0)
	->condition('jindiaojigou_id',$jindiaojigou_id)
  	->execute();
	$form_state['redirect'] = 'jindiaojigou/grade';	
}
function jindiaojigou_applyfl_page($id) {
	module_load_include("inc", "enterprise_information2","enterprise_information2.pages");	
	$page_content = '';
	$page_content .= l('返回列表','jindiaojigou/grade').'<br/>';
	$page_content .= enterprise_info_id($id);
	$render_array = array('#markup'=>$page_content);
	$form = drupal_get_form('jindiaojigou_apply_form',$id,1);
	$render_array['#markup'] .= drupal_render($form);
	return $render_array;
}
function jindiaojigou_applycw_page($id) {
	module_load_include("inc", "enterprise_information2","enterprise_information2.pages");	
	$page_content = '';
	$page_content .= l('返回列表','jindiaojigou/grade').'<br/>';
	$page_content .= enterprise_info_id($id);
	$render_array = array('#markup'=>$page_content);
	$form = drupal_get_form('jindiaojigou_apply_form',$id,0);
	$render_array['#markup'] .= drupal_render($form);
	return $render_array;
}

function get_users_jindiaojigou_id() {
	global $user;
	$query = 'SELECT id FROM {jindiaojigou_info} WHERE userid = ?';
	$query_result = db_query($query,array($user->uid))->fetchAssoc();
	$jindiaojigou_id = $query_result['id'];
	return $jindiaojigou_id;
}
function jindiaojigou_apply_form($form,&$form_state,$id,$type){
	$jindiaojigou_id = get_users_jindiaojigou_id();
	$query = 'SELECT * FROM {jindiaojigou_verdict} where jindiaojigou_id = ? and shenqingjijin_id = ?';
	$query_result = db_query($query,array($jindiaojigou_id,$id))->fetchAssoc();
	$verdict = $query_result;
	if($verdict['state']==22)
		drupal_goto('jindiaojigou/grade');
	$form['bid'] = array('#type'=>'hidden','#value'=>$id);
	$form['type'] = array('#type'=>'hidden','#value'=>$type);
	$form['zongheyijian'] = array(
		'#title' => t('综合意见'),
		'#type' => 'textarea',
		'#default_value' => $verdict['zongheyijian'],
	);
	$form['fengxiantishi'] = array(
		'#title' => t('风险提示'),
		'#type' => 'textarea',
		'#default_value' => $verdict['fengxiantishi'],
	);
	$form['upload'] = array(
	'#type' => 'managed_file',
	'#title' => '上传尽调报告',
	'#upload_location' => 'public://my_uploads',
	'#upload_validators' => array(
		'file_validate_extensions' => array('doc xls pdf docx xlsx zip tar.gz 7z'),
		'file_validate_size' => array(50*1024*1024)),
	);
	$form['file_name'] = array(
	'#title' => t('显示文件名：'),
	'#type' => 'textfield',
	'#default_value' => ($type == 0)?"财务尽调报告":"法律尽调报告",
	);	
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('暂存'),
	);
	return $form;
}
function jindiaojigou_apply_form_submit($form, &$form_state) {
	$values = $form_state['values'];
	$fid = $form_state['values']['upload']; 
	$file = file_load($fid);  
	$filename = $values['file_name'];
	if($filename == "")
		$filename = "附件";
	if ($file != null) {
		$saveurl = file_uri_target($file->uri);
	    $fid = $file->fid;
		$extension_errors=file_validate_extensions($file, "doc xls pdf docx xlsx zip tar.gz 7z");   
		if (!$extension_errors) {  
			$file->status = FILE_STATUS_PERMANENT;   
			file_save($file);
		}
		else {
			form_set_error('upload',$extension_errors[0]);
		}
	}
	$db_fields = array(
		'zongheyijian' => $values['zongheyijian'],
		'fengxiantishi' => $values['fengxiantishi'],
	);
	if($file != NULL){
		$db_fields['file_name'] = $filename;
		$db_fields['url'] = $saveurl;
		$db_fields['file_managed_id'] = $fid;
	}
	$jindiaojigou_id = get_users_jindiaojigou_id();
	$query = 'SELECT id FROM {jindiaojigou_verdict} WHERE jindiaojigou_id = ? AND shenqingjijin_id = ?';
	$query_result = db_query($query, array($jindiaojigou_id, $values['bid']))->fetchAssoc();
	if($query_result != FALSE){
		$verdict_id = $query_result['id'];
		db_update('jindiaojigou_verdict')->fields($db_fields)->condition('id',$verdict_id)->execute();
	}else{
		$db_fields['jindiaojigou_id'] = $jindiaojigou_id;
		$db_fields['shenqingjijin_id'] = $values['bid'];
		$db_fields['state'] = 0;
		$dv_fields['type'] = $values['type'];
		db_insert('jindiaojigou_verdict')->fields($db_fields)->execute();
	}
	$form_state['redirect'] = 'jindiaojigou/grade';
}
